home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c++-part1 / 7589 < prev    next >
Encoding:
Internet Message Format  |  1996-08-05  |  1.6 KB

  1. Path: library.erc.clarkson.edu!rpi!not-for-mail
  2. From: rjames@surf.com (R. James)
  3. Newsgroups: comp.lang.c++.moderated,comp.lang.c++
  4. Subject: Re: enum expression is IF statement
  5. Date: 23 Feb 1996 13:45:07 -0000
  6. Organization: Surf Communications, Inc.
  7. Sender: cppmods@netlab.cs.rpi.edu
  8. Approved: herbs@connobj.com
  9. Message-ID: <4gkgd3$ou9@netlab.cs.rpi.edu>
  10. References: <4fq7dc$pac@netlab.cs.rpi.edu> <4g1pj9$pqg@netlab.cs.rpi.edu>
  11. NNTP-Posting-Host: netlab.cs.rpi.edu
  12. X-Original-Date: Fri, 23 Feb 1996 02:04:44 GMT
  13.  
  14. gstern@earth.usa.net (Greg Sternberg/PDS Inc) wrote:
  15.  
  16. >Peter van Klaveren (P.v.Klaveren@bcs.cs.philips.com) wrote:
  17. >: enum
  18. >: {
  19. >:    CSEQ_IDLE,
  20. >:    CSEQ_ERROR,
  21. >:    CSEQ_ABORT,
  22. >:    CSEQ_WAIT4RESPONSE
  23. >: };
  24.  
  25. >: int state = 3;
  26.  
  27. >: if (state == CSEQ_WAIT4RESPONSE)
  28. >: {
  29. >:     // this code is not executed!!!
  30. >:     ...
  31. >: }
  32. IMO, the above is poor coding practice.
  33. Enums have two uses, pick either one, but do not mix them:
  34. Plan A (using enum to define constants):
  35.   enum
  36.   {
  37.      CSEQ_IDLE = 0,
  38.      CSEQ_ERROR = 1,
  39.      CSEQ_ABORT = 2,
  40.      CSEQ_WAIT4RESPONSE = 3
  41.   };
  42. should let you compare to ints
  43.  
  44. Plan B (using enum as an arbitrary set of codes):
  45.   typedef enum
  46.   {
  47.      CSEQ_IDLE,
  48.      CSEQ_ERROR,
  49.      CSEQ_ABORT,
  50.      CSEQ_WAIT4RESPONSE
  51.   } States;
  52.  
  53.   States state = CSEQ_WAIT4RESPONSE;
  54.  
  55.   if (state == CSEQ_WAIT4RESPONSE) ...
  56.  
  57.  
  58.  
  59.  
  60.       [ Articles to moderate: mailto:c++-submit@netlab.cs.rpi.edu ]
  61.       [  Read the C++ FAQ: http://www.connobj.com/cpp/cppfaq.htm  ]
  62.       [  Moderation policy: http://www.connobj.com/cpp/guide.htm  ]
  63.       [      Comments? mailto:c++-request@netlab.cs.rpi.edu       ]
  64.